<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计划管理</title>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css"></link>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/xtheme-slate.css"></link>
<script src="../ext/adapter/ext/ext-base.js"></script>
<script src="../ext/ext-all.js"></script>
<script src="../ext/src/locale/ext-lang-zh_CN.js"></script>	
<script src="../ext/base/PagingMemoryProxy.js"></script>
<script type="text/javascript">
			Ext.onReady(function(){
				Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);      
				function checkUserSessionStatus(conn,response,options){      
				    if(response.getResponseHeader('sessionstatus')){      
				    	parent.location.href='/xdj/index.jsp';   
				    }      
				}  
			//	Ext.BLANK_IMAGE_URL='ext/resources/images/default/s.gif';
				var planNode;
				var lineNode;
				var zoneNode;
				var treeloader = new Ext.tree.TreeLoader({   // dataUrl : 'tree.jsp'//这里可以不需要指定URL，在加载前事件响应里面设置 	
				});  
				var rootnode = new Ext.tree.AsyncTreeNode({  // 异步加载根节点 
					id : '-1',  
					text : '大坝电厂',  
					cls : 'folder' 
				});  
				var thenode = rootnode;
				var tree=new Ext.tree.TreePanel({
					el : 'tree',// 将树形添加到一个指定的div中,非常重要！  
					border:false,
					region : 'west',  
					//title : '功能菜单',  
					lines:false, //线段
					width : 180, 
					height:window.screen.height,
					split : true, 
					//autoHeight : true,  
					frame : true,// 美化界面    
					enableDD : false,// 是否支持拖拽效果  
					//containerScroll : true,// 是否支持滚动条  
				//	autoScroll:true,
					rootVisible : false, // 是否隐藏根节点,很多情况下，我们选择隐藏根节点增加美观性  
					animate : true, // 动画效果  
					root : rootnode,
					loader : treeloader ,
					listeners : {
						'beforeload' : function(node) {
							treeloader.dataUrl = "plan_loadtree?nodeDepth="+node.getDepth()+"&nodeId="+node.id;
						},
						'click' : function(node) {
							thenode = node ;
							if(node.getDepth()==3) {// zone
								zoneNode = node;
								lineNode = node.parentNode;
								planNode = node.parentNode.parentNode;
								
								var chkdate = Ext.getCmp("dutyDate").getValue();
				        		if(chkdate==null||chkdate=='') {
				        			chkdate = new Date();
				        			chkdate.setHours(0, 0, 0, 0);
				        		}
								store.load({
									params:{
										chkPlanID:planNode.id.split("#")[1],
										chkLineID:lineNode.id.split("#")[2],
										zoneID:zoneNode.id.split("#")[3],
										dutyDate:chkdate.format('Y-m-d'),
									}
								});
							}
						}
					}
					
				// 树加载  
				});  
				tree.render();  // 渲染树形 
				var contextmenu=new Ext.menu.Menu({
					id:'contextmenu',
					items:[{
						text: '导入',disabled:true,id:'cToolImport',
				        handler:function(){ 
				        	if(thenode.getDepth()==1||thenode.getDepth()==2) {
				        		showImportWin("plan_importNodes");
				        	}
				        }
						},
						{
							text: "删除",disabled:true,id:'cToolDelete',
					    	handler:function() {
								if(thenode.getDepth()==2||thenode.getDepth()==3) {
									Ext.Ajax.request({
										url:'plan_deleteNode',
										success:function(response){
											var result = Ext.util.JSON.decode(response.responseText);
											if(result.success==true){
												tnode = thenode;
												thenode = thenode.parentNode;
												tnode.remove(true);
												window.location.reload();
											}else{
												Ext.Msg.alert("提示","操作失败，请重试。") ;
											}
										},
										params:{
											nodeId:thenode.id,
											nodeDepth:thenode.getDepth(),
											parNodeId:thenode.parentNode.id
										}
									});
								}

					    	}
						}
					]
				});
				tree.on('contextmenu',function(node,e){
					e.preventDefault();//屏蔽掉系统浏览器菜单
					node.select();
					thenode=node;
					contextmenu.showAt(e.getXY());
				}); 
				function showImportWin(urlString) {
					//表格
					var sm = new Ext.grid.CheckboxSelectionModel();//列的复选框
					var cm=new Ext.grid.ColumnModel([
						sm, //加到每列的前边
						{header:thenode.getDepth()==1?"路线名称":"区域名称",dataIndex:'name',width:30},
						{header:'id',dataIndex:'id',hidden:true}
					]);//定义相应的列模型
				
					var importStore=new Ext.data.JsonStore({
						url:'plan_getUnimportNodes',
						fields:['id','name']
					});//存储前两项的定义的东西，将其连接起来
					importStore.load({
						params:{
							nodeDepth:thenode.getDepth()
						}
					});
					var importpanel = new Ext.grid.GridPanel({
						cm:cm,
						sm:sm,
						store:importStore,
						width:150,
						height:150, //设置表格的高度 true为自动获取表格的大小
						renderTo:'grid' , //渲染容器
						enableColumnMove:false,//不可以使表格列移动
						enableColumnResize:false, //不可以改变列的宽度
						stripeRows:false, //True表示为显示行的分隔符
						loadMask:true,  //会有一个Ext.LoadMask的遮罩效果
						trackMouseOver : true,  //True表示为鼠标移动时高亮显示（默认为true）。
						enableDragDrop : true,
						viewConfig:{
							forceFit:true //让没列自动填满表格，根据你设置的宽度按比例分配
						}
					});
					someform=new Ext.form.FormPanel({
					frame:true,
					height:350,
					buttonAlign:'top',
					layout:'form',
					items:[{
						xtype:"panel",
				        layout:"column",//也可以是table,实现多列布局
				       // fieldLabel:'归属值次',
				        isFormField:true,//非常重要,否则panel默认不显示fieldLabel
				        items:[importpanel]
					},
					{
						xtype:'hidden',
						name:'importIds'
					}
					],
					buttons:[{
						text: "导入",
					       handler:function(){ 
					    	   var oSelMode    = importpanel.getSelectionModel();
					    	   oRecords    = oSelMode.getSelections();
					    	   var sIds = "";
					    	   for(var i=0;i<oRecords.length;i++){
						    	   sIds += oRecords[i].get("id")+",";
						    	   }
					    	   sIds = sIds.substring(0, sIds.length-1);
					    	   Ext.Ajax.request({
					    		   url:urlString,
									params:{
										nodeId:thenode.id,
										nodeDepth:thenode.getDepth(),
										importIds:sIds
									},
									success:function(response){
										var result = Ext.util.JSON.decode(response.responseText);
										if(result.success==true){
											win.close();
							        		window.location.reload();
										}else{
											Ext.Msg.alert("提示","操作失败，请重试。") ;
										}
									}
								});
							}
				     },{
				        text: "取消",
				        handler:function(){ 
				           	win.close() ;
				        }
				     }]
					});
					var win=new Ext.Window({
							title:'添加值班人员',
							 width:400,
							 //height:300,
							 items:[someform],
							 xtype:'form'
					});
					win.show();
				}
				
				//var sm = new Ext.grid.CheckboxSelectionModel();//列的复选框
				var cm=new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(),  //自动增加序列号
					//sm, //加到每列的前边
					{header:'设备名称',dataIndex:'DevName',sortable:true}, //True表示为可在该列上进行排列
					{header:'巡检项目',dataIndex:'CHKItem'},
					{header:'值次名称',dataIndex:'DutyName'},
					{header:'值班名称',dataIndex:'ClaName'},
					{header:'轮次序号',dataIndex:'TurnOrder'},
					{header:'巡检人员',dataIndex:"UserName"},
					{header:'起始时间',dataIndex:'Start_PDA_CHKDateTime',width:200},
					{header:'终止时间',dataIndex:'End_PDA_CHKDateTime',width:200}
				]);//定义相应的列模型
				var store=new Ext.data.JsonStore({
				 	url: 'plan_getTaskByZone',
				 	//autoLoad : false,
				 	 root: 'result',
				 	// idProperty:'id',
					totalProperty:'totalProperty',
				    fields: ["DevName",
				             "CHKItem",
				             "DutyName",
				             "ClaName",
				             "TurnOrder",
				             "UserName",
				             "Start_PDA_CHKDateTime",
				             "End_PDA_CHKDateTime"]
				});//存储前两项的定义的东西，将其连接起来
				                         	
				var panel = new Ext.grid.GridPanel({
					region:'center',
					border:false,
				    cm:cm,
				    //sm:sm,
				    store:store,
				    height:document.body.clientHeight-50,  //设置表格的高度 true为自动获取表格的大小
				    renderTo:'grid' , //渲染容器
				    enableColumnMove:false,//不可以使表格列移动
				    enableColumnResize:false, //不可以改变列的宽度
				    stripeRows:false, //True表示为显示行的分隔符
				    loadMask:true,  //会有一个Ext.LoadMask的遮罩效果
				    trackMouseOver : true,  //True表示为鼠标移动时高亮显示（默认为true）。
				    enableDragDrop : true,
				    viewConfig:{
				      forceFit:true //让没列自动填满表格，根据你设置的宽度按比例分配
				    },
				    
				    tbar:new Ext.PagingToolbar({
				    	pageSize : 10, //每页显示几条
						store : store,
						beforePageText:"当前第",   
			            afterPageText:"页，共{0}页",   
			            lastText:"尾页",   
			            nextText :"下一页",   
			            prevText :"上一页",   
			            firstText :"首页",                           
				        displayInfo:true, 
				        displayMsg:'显示第{0}条数据到{1}条数据,一共有{2}条', 
				        emptyMsg:'没有记录'
				  })
				 });
				store.load({params:{start:0,limit:10}});
				var planform=new Ext.form.FormPanel({
					
					frame:true,
					height:50,
					buttonAlign:'top',
					labelAlign:'right',
					layout:'form',
// 					buttons:[{
// 						text:'查询'
// 					},{
// 						text:'导出EXCEL'
// 					}],
					items:[{
						layout:'column',
						items:[{
							layout:'form',
							labelWidth:60,
							items:[{
								id:'dutyDate',
								fieldLabel:'巡检时间',
								width:150,
								xtype:'datefield',
								format : 'Y-m-d',
								listeners : { 
							        change : function(m, n, o) {
							        	//m.focus(false,1000);
							        	var str = planform.getForm().findField("dutyDate").getValue().format('Y-m-d');
							        	if(thenode.getDepth()==3) {
							        		store.load({
												params:{
													chkPlanID:planNode.id.split("#")[1],
													chkLineID:lineNode.id.split("#")[2],
													zoneID:zoneNode.id.split("#")[3],
													dutyDate:str
												}
											});
							        	}
							        }
							    } 
							}]
						}]
					}]
				});
				planform.getForm().findField("dutyDate").setValue(new Date());
				 new Ext.Viewport({  
					layout : 'border', // 使用border布局  
					border:false,
					items:[{
						region:'west',
						autoScroll : true,
						border:false,
						height:window.screen.height,
						width:180,
						items:[tree]
					},{
						region:'center',
						height:window.screen.height,
						border:false,
						items:[planform,panel]
					}],
					listeners:{
						'beforerender' : function(cmp) {
							// set role
							var store=new Ext.data.JsonStore({
								url: '../system/role_loadFunc?funcCode=ControlPlanManagement',
								autoLoad : true,
								fields: ["functioncode"]
							});
							store.load({
							    callback: function(records, options, success){
							    	var i=0;
							    	for(; i<records.length; i++) {
							    		var id = records[i].get("functioncode");
							    		//alert('d'+Ext.getCmp('56').text);
							    		if(Ext.getCmp(id)!=null) {
							    			Ext.getCmp(id).setDisabled(false);
							    		}
							        }
							    },
							    scope: store
							});
						}
					}
				 }); 
			});
		
	</script> 
</head>
<body>
<div id="tree" ></div>
<div id="grid"></div>
</body>
</html>